﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;

class NetDataHander
{
    private Mutex m_dataLock = new Mutex();
    private List<byte[]> m_lsData = new List<byte[]>();
    public void AddData(byte[] pData)
    {
        m_dataLock.WaitOne();
        m_lsData.Add(pData);
        m_dataLock.ReleaseMutex();
        HandleData();
    }
    public bool IsHaveData()
    {
        bool ret = false;
        m_dataLock.WaitOne();
        ret = m_lsData.Count > 0;
        m_dataLock.ReleaseMutex();
        return ret;
    }

    public byte[] GetData()
    {
        byte[] ret = null;
        m_dataLock.WaitOne();
        if (m_lsData.Count > 0)
        {
            ret = m_lsData[0];
            m_lsData.RemoveAt(0);
        }
        m_dataLock.ReleaseMutex();
        return ret;
    }
    public int GetDataCount()
    {
        int ret = 0;
        m_dataLock.WaitOne();
        ret = m_lsData.Count;
        m_dataLock.ReleaseMutex();
        return ret;
    }
    void HandleData()
    {
        if (!IsHandle())
        {
            if(GetDataCount() != 0)
            {
                SetHandle(true);
                Thread t = new Thread(new ThreadStart(this.HandleDatatThread));
                t.Start();
            }
        }
    }

    bool IsHandle()
    {
        int iRet = 0;
        Interlocked.Exchange(ref iRet, m_isHandle);
        return iRet == 1 ? true : false;
    }
    void SetHandle(bool IsHandle)
    {
        Interlocked.Exchange(ref m_isHandle, IsHandle == true ? 1 : 0);
    }

    int m_isHandle = 0;
    public void HandleDatatThread()
    {
        byte[] pData;
        do
        {
            pData = GetData();
            if(pData != null) 
            {
                SetHandle(true);
                HandleDatatCore(pData);
            }
        } while (pData != null);
        SetHandle(false);
    }
    public void HandleDatatCore(byte[] pData)
    {
        String strError="";
        int iLength = NetCommandDefine.GetDataLength(ref pData);
        int iFrom = NetCommandDefine.GetDataFrom();
        StreamLoadData fp = new StreamLoadData(ref pData, iFrom, iLength);
        String strObjName = "";
        String strEndObj = "End Obj";
        while ((!fp.IsEof()))
        {
            strObjName = "";
            fp.Read(ref strObjName);

            while ((!fp.IsEof()) && strObjName.Length > 0)
            {
                String strFuncName = "";
                fp.Read(ref strFuncName);
                if (String.Equals(strFuncName, strEndObj))
                {
                    break;
                }
                if (strFuncName.Length != 0)
                {
                    int iParamNum = 0;
                    fp.Read(ref iParamNum);
                    object[] lsObject = new object[iParamNum];
                    for (int i = 0; i < iParamNum; ++i)
                    {
                        String strParamName = "";
                        fp.Read(ref strParamName);
                        Type objType = Type.GetType(strParamName);
                        lsObject[i] = (NameCreate.CreateClass<object>(strParamName));

                        fp.Read(ref lsObject[i]);
                    }
                    Type[] lsType = new Type[] { typeof(String), typeof(object[]) };
                    MethodInfo m = NameCreate.CreateMethod(strFuncName, lsType, "CommandFunction");
                    if (m != null)
                    {
                        try
                        {
                            m.Invoke(null, new object[] { strObjName, lsObject });
                        }
                        catch (System.Exception ex)
                        {
                            strError = ex.ToString();
                        }

                    }
                }
            }
        }
      
        

        HandleData();
    }

}